A Lightweight PowerShell Script to Update Google Domains’ Dynamic DNS

 

[UPDATE 2022-12-25: I no longer recommend using Google DNS. I suppose I don't recommend *against* using it either, but that's only because as we've discovered in recent years, there doesn't appear to be a technology company with the resources to provide internet DNS resolution that isn't going to do some combination of (a) track any and all user data that touches it, (b) sell that data, (c) use that data to build a derivative item that it can sell, (d) refuse to sell that data but accidentally have it all stolen and leaked online, and (e) more or less lie about whether or not they're doing any of that. At this point I think it's safe to say that if you use the internet you're going to be leaking data like a sieve and so, while Google DNS resolution probably is less freewheeling with how they handle information than like, whatever name resolution servers your internet provider's modem defaults to, they're all basically indiscernible from one another.]

First of all, Google DNS is a fantastic deal and while it’s a little different to work with than other Domain Name providers, it’s surprisingly quick and if you’re someone who routinely specifies Google’s DNS servers to override whatever crap your ISP provides like I do (they’re 8.8.8.8 and 8.8.4.4 in case you’re wondering) changes you make through the administration page are (in my case) virtually instant.  I also haven’t noticed much of a time delay in actual propagation using various online tools to check that things were resolving correctly so there’s really no downside.  The fact that private registration is included in the fee is also pretty awesome.

Originally I only wanted a domain so I could route traffic back to my home network over a VPN but I assumed getting a static IP from my ISP would be easier (read: more affordable) than it really is.  Unfortunately, I didn’t check this before I purchased the domain so had a brief panic attack before realizing that Google Domains supports dynamic IP updates.

Basically, you create a subdomain name and Google creates a record for it and gives you a unique username and password.  You then use that with (ostensibly) third party dynamic DNS update clients or you can browse to a specific page and Google will use the IP you access that page from.

I figured the latter would be easier since I was having trouble with other update clients (plus with limited system resources, sadly having even a tiny dynamic DNS updater in my taskbar wasn’t worth the performance hit) and figured I’d just come up with my own way of updating things with a scheduled task.

Turns out this is harder than it sounds.  IE on Server 2012 R2 doesn’t care to launch to a specific webpage for some reason and even more irritatingly it doesn’t consider the URL string of Google’s IP update page a valid web-address (you can’t set it as you homepage).  As I tend to do, when I think something should be simple and it turns out to have more roadblocks than necessary in the way, I get angry and don’t leave the computer screen until I have a working solution.  It’s a character flaw.

After reading up on some PowerShell commands I realized it shouldn’t be hard to have a basic .ps1 script with the username, password, and unique update URL and just have that run at hourly intervals through Task Scheduler.  Unfortunately Google’s API requires that the URL is accessed by some client that provides its browser-type.  I can see why Google requires this but it meant just using GET or POST wasn’t going to work.

Finally, after combing the web, I was able to find a way to create a browser session in PowerShell that actually reports a browsertype to the requested site.  It was late at night and I can’t find the original post – I know it was on StackExchange and I’d like to give them credit but I really can’t find it again. 

$username = "USERNAMEGOESHERE"

$password = "PASSWORDGOESHERE"

$url = "https://domains.google.com/nic/update?hostname=[SUBDOMAINGOESHERE]"

$webclient = new-object System.Net.WebClient

$webclient.Credentials = new-object System.Net.NetworkCredential($username, $password)

$webpage = $webclient.DownloadString($url)

$webpage

Start-Sleep -s 10

exit 

 

Everything should be pretty self-explanatory; use the username and password Google provides when you create the dynamic DNS subdomain in the web console for the first two entries and in the [SUBDOMAIN GOES HERE] box put the subdomain you created (the whole thing, so: subdomain.yourdomain.whatever).  You can delete the Start-Sleep –s 10 command if you like, it’s only there for testing purposes; you won’t be able to see any errors the command returns if something’s wrong.

Then just create a scheduled task to run on whatever interval you like and point it at the .ps1 file.  Easy peasy.

Oh – if you notice it’s not running, it may be your PowerShell execution policy.  Mind the obvious security implications here, but the following command in an elevated PowerShell instance should allow the script (and any other PowerShell executable script) to run:

Set-ExecutionPolicy Unrestricted

 

Best of luck and let me know if you have any problems – I can’t guarantee I’ll get back to you, but it’s always worth a shot.

P.s. if you posted on StackExchange something similar to this for another purpose, please let me know so I can put a link and reference here; I definitely wouldn’t have figured this out on my own and would like to acknowledge my sources.